package com.xbf.controller;

import com.xbf.entity.Order;
import com.xbf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public Map<String, Object> createOrder(@RequestBody Order order) {
        Map<String, Object> result = new HashMap<>();
        boolean success = orderService.createOrder(order);
        if (success) {
            // 返回创建的订单ID
            result.put("success", true);
            result.put("message", "订单创建成功");
            result.put("data", order); // 返回完整的订单对象
        } else {
            result.put("success", false);
            result.put("message", "订单创建失败");
        }
        return result;
    }

    @GetMapping("/user/{userId}")
    public List<Order> getOrdersByUserId(@PathVariable Integer userId) {
        return orderService.getOrdersByUserId(userId);
    }

    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Integer id) {
        return orderService.getById(id);
    }

    @PutMapping("/update")
    public boolean updateOrder(@RequestBody Order order) {
        return orderService.updateById(order);
    }

    @DeleteMapping("/{id}")
    public boolean deleteOrder(@PathVariable Integer id) {
        return orderService.removeById(id);
    }
}